package geobeans.cn.demo;

import android.app.Activity;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import cn.geobeans.android.input.AndroidMotionEvent;
import cn.geobeans.backend.CanvasAdapter;
import cn.geobeans.backend.canvas.Bitmap;
import cn.geobeans.common.GeoPoint;
import cn.geobeans.event.Gesture;
import cn.geobeans.layers.GPathLayer;
import cn.geobeans.layers.GPolygonLayer;
import cn.geobeans.layers.ItemizedLayer;
import cn.geobeans.layers.MarkerItem;
import cn.geobeans.layers.MarkerSymbol;
import cn.geobeans.layers.OnItemGestureListener;
import cn.geobeans.map.Map;
import cn.geobeans.map.ViewController;

/**
 * Created by ${LZD} on 2016/11/29.
 */
public class PGISEventHandler implements GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener, OnItemGestureListener {
    private final AndroidMotionEvent mMotionEvent;
    private final Map mMap;
    private ViewController mViewport;
    private Activity context;

    private GeoPoint rect1;
    private GeoPoint rect2;

    public PGISEventHandler(Map map, Activity context) {
        mMotionEvent = new AndroidMotionEvent();
        mMap = map;
        mViewport = mMap.viewport();
        this.context = context;


    }

    //采集相关代码
    private int caijioverlayType = -1;

    public void setCaijiOperation(int type) {
        caijioverlayType = type;
        if (caijioverlayType >= 1) {
            initPolygon();
            initMark();
            initPath();
        }
    }

	/* GesturListener */

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        //	return mMap.handleGesture(Gesture.TAP, mMotionEvent.wrap(e));
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {
        mMap.handleGesture(Gesture.LONG_PRESS, mMotionEvent.wrap(e));
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        return false;
    }

    @Override
    public boolean onDown(MotionEvent e) {
        return mMap.handleGesture(Gesture.PRESS, mMotionEvent.wrap(e));
    }

    /* DoubleTapListener */
    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        GeoPoint gp = mViewport.fromScreenPoint(e.getX(), e.getY());
        if (caijioverlayType == 1)
            addMark(gp.getLatitude(), gp.getLongitude());
        else if (caijioverlayType == 2)
            addLinePt(gp);
        else if (caijioverlayType == 3)
            addPolygonPt(gp);
        else if (caijioverlayType == 4) {
            if (rect1 == null) {
                rect1 = gp;
            } else {
                rect2 = gp;
                addRect(rect1, rect2);
                rect1 = null;
                rect2 = null;
                caijioverlayType = -1;
            }
        }
        mMap.updateMap(true);
        return mMap.handleGesture(Gesture.TAP, mMotionEvent.wrap(e));
    }
    GPolygonLayer polyLayer;
    private void addRect(GeoPoint gp1, GeoPoint gp2) {
        List<GeoPoint> pts = new ArrayList<GeoPoint>();
//        String strpath = "115.325,37.0567, 115.325,37.0667,115.335,37.0667,115.335,37.0567,115.325,37.0567";
//        StringTokenizer st = new StringTokenizer(strpath, ",");
//        while (st.hasMoreTokens()) {
//            double lon = Double.parseDouble(st.nextToken());
//            double lat = Double.parseDouble(st.nextToken());
//            pts.add(new GeoPoint(lat, lon));
//        }
        pts.add(new GeoPoint(gp1.getLatitude(), gp1.getLongitude()));
        pts.add(new GeoPoint(gp2.getLatitude(), gp1.getLongitude()));
        pts.add(new GeoPoint(gp2.getLatitude(), gp2.getLongitude()));
        pts.add(new GeoPoint(gp1.getLatitude(), gp2.getLongitude()));
        pts.add(new GeoPoint(gp1.getLatitude(), gp1.getLongitude()));

        polyLayer = new GPolygonLayer(mMap, 0xffff0000, 3, 0x440000ff);
        mMap.layers().add(polyLayer);
        polyLayer.setPoints(pts);
        mMap.render();
    }


    @Override
    public boolean onDoubleTapEvent(MotionEvent e) {
        return false;
    }

    @Override
    public boolean onDoubleTap(MotionEvent e) {
        if (caijioverlayType >= 1) {
            caijioverlayType = -1;
            return false;
        } else
            return mMap.handleGesture(Gesture.DOUBLE_TAP, mMotionEvent.wrap(e));
    }

    ItemizedLayer markerLayer;

    //初始化点
    public void initMark() {
        if (markerLayer == null) {
            Bitmap bitmap = CanvasAdapter.getBitmapAsset("1.png");
            MarkerSymbol symbol;
            symbol = new MarkerSymbol(bitmap, 0.5f, 0.5f);
            markerLayer = new ItemizedLayer(mMap, new ArrayList<MarkerItem>(),
                    symbol, this);
            mMap.layers().add(markerLayer);
        }
    }

    //添加点
    public void addMark(double lat, double lon) {
        markerLayer.addItem(new MarkerItem(lat + "/" + lon, "", new GeoPoint(lat, lon)));
        mMap.render();
    }

    GPathLayer pathLayer;
    //初始化线
    private void initPath() {
        if (pathLayer == null) {
            int c = 0xFF00FF00;
            pathLayer = new GPathLayer(mMap, c, 2, this, "line1"); //线色， 线宽，id
            mMap.layers().add(pathLayer);
        }
    }
    //添加线
    public void addLinePt(GeoPoint pt) {
        pathLayer.addPoint(pt);
        mMap.render();
    }

    public void setLinePts(List<GeoPoint> pts) {
        pathLayer.setPoints(pts);
        mMap.render();
    }

    GPolygonLayer polygonPath;
    //初始化面
    public void initPolygon() {
        if (polygonPath == null) {
            //线色，线宽，面色，id
            polygonPath = new GPolygonLayer(mMap, 0xff00ff00, 3, 0x440000ff, this, "polygon1");
            mMap.layers().add(polygonPath);
        }
    }
    //添加面
    public void addPolygonPt(GeoPoint pt) {
        polygonPath.addPoint(pt);
        mMap.updateMap(true);
    }

    //Mark点击事件处理
    public boolean onItemSingleTapUp(int index, Object obj) {
        if (caijioverlayType == -1) {
            Toast toast = null;
            if (obj instanceof MarkerItem) {
                MarkerItem item = (MarkerItem) obj;
                toast = Toast.makeText(context, item.getTitle(), Toast.LENGTH_SHORT);
            } else if (obj instanceof GPathLayer) {
                GPathLayer pathLayer = (GPathLayer) obj;
                toast = Toast.makeText(context, pathLayer.getId(), Toast.LENGTH_SHORT);
            } else if (obj instanceof GPolygonLayer) {
                GPolygonLayer polygonLayer = (GPolygonLayer) obj;
                toast = Toast.makeText(context, polygonLayer.getId(), Toast.LENGTH_SHORT);
            }
            toast.show();
        }
        return false;
    }

    public boolean onItemLongPress(int index, Object item) {

        return false;
    }

    public List<GeoPoint> getPolyData() {
        if (polygonPath == null) return null;
        return polygonPath.getPoints();
    }

    public void polygonClosed() {
        polygonPath.addPoint((GeoPoint) polygonPath.getPoints().get(0));
//        mWkt = MapViewUtils.getGPolygonLayerWkt(polygonPath);
    }

    public void clear() {
        mMap.layers().remove(pathLayer);
        mMap.layers().remove(markerLayer);
        mMap.layers().remove(polygonPath);
        mMap.layers().remove(polyLayer);
        pathLayer = null;
        markerLayer = null;
        polygonPath = null;
        polyLayer = null;
        mMap.updateMap(true);
        caijioverlayType = -1;
    }
}
